
import { reactive, ref, computed, effect, EffectScope, onScopeDispose } from '../../reactivity';
import { core_model } from '../model/core_model';
import { Log } from '../logger/LoggerGlobal';

/**
 * ViewModel基类 - MVVM架构中的核心组件
 * 负责管理视图状态、业务逻辑和数据绑定
 */
export class core_viewmodel {
    // 响应式数据存储
    protected _data: Record<string, any> = {};
    
    // 计算属性存储
    protected _computed: Record<string, any> = {};
    
    // 命令存储
    protected _commands: Record<string, Function> = {};
    
    // 效果作用域
    protected _scope: EffectScope;
    
    // 关联的Model
    protected _model: core_model | null = null;
    
    // 是否已初始化
    protected _initialized: boolean = false;


    
    constructor() {
        this._scope = new EffectScope();
        this._data = reactive({});
        this.initialize();
        this.initializeData();
        this.initializeComputed();
        this.initializeCommands();
    }
    
    /**
     * 初始化响应式数据
     * 子类需要重写此方法来定义数据属性
     */
    protected initializeData(): void{
        
    }
    
    /**
     * 初始化计算属性
     * 子类可以重写此方法来定义计算属性
     */
    protected initializeComputed(): void {
        // 子类可以重写
    }
    
    /**
     * 初始化命令
     * 子类可以重写此方法来定义命令
     */
    protected initializeCommands(): void {
        // 子类可以重写
    }

    get data():Record<string, any> {
        return this._data;
    }
    
    /**
     * 获取数据属性
     */
    public getData<T = any>(key: string): T {
        return this._data[key];
    }
    
    /**
     * 设置数据属性
     */
    public setData<T = any>(key: string, value: T): void {
        this._data[key] = value;
    }
    
    /**
     * 获取计算属性
     */
    public getComputed<T = any>(key: string): T {
        return this._computed[key];
    }
    
    /**
     * 添加计算属性
     */
    protected addComputed<T = any>(key: string, getter: () => T): void {
        this._computed[key] = computed(getter);
    }
    
    /**
     * 添加命令
     */
    protected addCommand(name: string, command: Function): void {
        this._commands[name] = command;
    }
    
    /**
     * 执行命令
     */
    public executeCommand(name: string, ...args: any[]): any {
        const command = this._commands[name];
        if (command) {
            return command.apply(this, args);
        } else {
            Log.mvvm.warn('命令未找到', { commandName: name });
        }
    }
    
    /**
     * 设置关联的Model
     */
    public setModel(model: core_model): void {
        this._model = model;
        this.onModelChanged();
    }
    
    /**
     * 获取关联的Model
     */
    public getModel(): core_model | null {
        return this._model;
    }
    
    /**
     * Model变化时的回调
     */
    protected onModelChanged(): void {
        // 子类可以重写
    }
    
    /**
     * 添加副作用
     */
    public addEffect(fn: () => void): void {
        this._scope.run(() => {
            effect(fn);
        });
    }
    
    /**
     * 初始化ViewModel
     */
    public initialize(): void {
        if (this._initialized) {
            return;
        }
        
        this._initialized = true;
        this.onInitialize();
    }
    
    /**
     * 初始化完成后的回调
     */
    protected onInitialize(): void {
        // 子类可以重写
    }
    
    /**
     * 销毁ViewModel
     */
    public destroy(): void {
        this.onDestroy();
        this._scope.stop();
        this._model = null;
        this._initialized = false;
    }
    
    /**
     * 销毁前的回调
     */
    protected onDestroy(): void {
        // 子类可以重写
    }
    
    /**
     * 获取所有数据属性
     */
    public getAllData(): Record<string, any> {
        return { ...this._data };
    }
    
    /**
     * 获取所有计算属性
     */
    public getAllComputed(): Record<string, any> {
        return { ...this._computed };
    }
    
    /**
     * 获取所有命令
     */
    public getAllCommands(): Record<string, Function> {
        return { ...this._commands };
    }
    
    /**
     * 检查是否已初始化
     */
    public isInitialized(): boolean {
        return this._initialized;
    }
}
